<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.mobile" placeholder="请输入用户手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" circle @click="getDataList()">查询</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">添加用户</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label=""
        v-if='false'>
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="用户名称">
      </el-table-column>
      <!-- <el-table-column label="所属代理">
        <template slot-scope="scope">
          <p>{{ scope.row.parentUser_name }}</p>
          <p>{{ scope.row.parentUser_mobile }}</p>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="手机号码">
      </el-table-column>
        <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="注册时间">
      </el-table-column>
      <el-table-column
        prop="status.desc"
        header-align="center"
        align="center"
        label="用户状态">
      </el-table-column>
      <el-table-column
        prop="balance"
        header-align="center"
        align="center"
        label="余额">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)" >修改</el-button>
          <el-button type="text" size="small" @click="addMoneyHandle(scope.row.id)">充值</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top:12px;"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalSize"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
     <add-money v-if="addMoneyVisible" ref="addMoney" @refreshDataList="getDataList"></add-money>
  </div>
</template>

<script>
  import API from '@/api'
  import AddOrUpdate from './add-or-update'
  import addMoney from './add-money'
  export default {
    data () {
      return {
        dataForm: {
          mobile: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalSize: 0,
        addMoneyVisible: false,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      addMoney
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        var params = {
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            mobile_like: this.dataForm.mobile
        }
        API.agencyminiuser.list(params).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            this.dataList = data.page.content
            this.totalSize = data.page.totalSize
          } else {
            this.dataList = []
            this.totalSize = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 迁移用户(修改)
      addOrUpdateHandle (id) {
        // if(id){
          // 修改 调用方法修改parentUserId
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(id)
          })
      },
      addMoneyHandle (id) {
        if(id){
          this.addMoneyVisible = true
          this.$nextTick(() => {
            this.$refs.addMoney.init(id)
          })
        }
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          API.agencyminiuser.del(ids).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
